import axios from 'axios'
import store from '../store'

const instance = axios.create({
    // baseURL: '/api',
    baseURL: 'http://47.100.78.238:89/api'
    // timeout: 10000
})

let num = 1;

// 封装请求拦截器
instance.interceptors.request.use(config => {
    store.commit('showLoading')
    num++
    // console.log(num)
    return config
}, err => {
    store.commit('hideLoading')
    num = 1
    this.$Message.error('网络异常，请稍后再试')
    return Promise.reject(err)
})

// 封装响应拦截器
instance.interceptors.response.use(response => {
    num--
    if(num == 1){
        store.commit('hideLoading')
    }
    // console.log(num)
    
    return response
}, err => {
    store.commit('hideLoading')
    num = 1
    this.$Message.error('网络异常，请稍后再试')
    return Promise.reject(err)
})

// 封装通用请求
export function request(config) {
    return  new Promise((resolve, reject) => {
        instance(config).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}